package ex;

/**
 * @author xizizzz
 * @description: TODO
 * @date 2021-9-7下午 05:41
 */
public class quicksort {
    public static void main(String[] args) {
        final int[] ints = {1, 2, 3, 4};

        quicksort(ints, 0, ints.length - 1);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

    public static void quicksort(int[] num, int l, int r) {
        if (l >= r) return;
        int x = num[l], i = l - 1, j = r + 1;
        while (i < j) {
            //从小到大排序
            do {
                i++;
            } while (num[i] < x);
            do {
                j--;
            } while (num[j] > x);
            if (i < j) {
                int temp = num[i];
                num[i] = num[j];
                num[j] = temp;
            }
        }
        quicksort(num, l, j);
        quicksort(num, j + 1, r);
    }
}
